package cc.autoapi.pucong.auto.flow.spring.boot.start.config;

import cc.autoapi.pucong.auto.flow.core.root.properties.AutoFlowDesignConfiguration;
import cc.autoapi.pucong.auto.flow.core.root.utils.EnumConvertFactory;
import cc.autoapi.pucong.auto.flow.spring.boot.start.AutoServiceAutoConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.List;


@Configuration
@AutoConfigureAfter({AutoServiceAutoConfiguration.class})
public class AutoFlowDesignWebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    private AutoFlowDesignConfiguration autoFlowDesignConfiguration;

    private final DesignCorsFilter designCorsFilter = new DesignCorsFilter();

    private boolean registerInterceptor = false;

    private boolean registerMapping = false;

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(mappingJackson2HttpMessageConverter());
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        boolean enable = autoFlowDesignConfiguration.isEnable();
        if (enable) {
            String web = autoFlowDesignConfiguration.getWebPath();
            if (web != null && !registerMapping) {
                registerMapping = true;
                // 配置静态资源路径
                registry.addResourceHandler(web + "/**").addResourceLocations("classpath:/auto-flow-web/");
            }
        }
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverterFactory(new EnumConvertFactory());
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (!registerInterceptor) {
            registerInterceptor = true;
        }
        registry.addInterceptor(new LoginInterceptor(autoFlowDesignConfiguration, autoFlowDesignConfiguration.isSupportCrossDomain() ? designCorsFilter : null))
                .addPathPatterns("/**")
                .excludePathPatterns("/sys/login/login")
                .excludePathPatterns("/sys/login/isLogin")
                .excludePathPatterns("/api/exec/apiExec/**")
                .excludePathPatterns("/api/exec/apiExecStream/**")
                .excludePathPatterns("/views/**")
                .excludePathPatterns("/views/index.html")
                .excludePathPatterns("/views/fonts/**")
                .excludePathPatterns("/views/img/**")
                .excludePathPatterns("/views/js/**");
    }

    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter ret = new MappingJackson2HttpMessageConverter();
        ret.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON_UTF8, MediaType.TEXT_HTML,MediaType.APPLICATION_OCTET_STREAM));
        ret.setObjectMapper(new JacksonCustomObjectMapper());
        return ret;
    }

}
